/******************************************************************************* 
 * Copyright (C) 2012-2015 Microfountain Technology, Inc. All Rights Reserved. 
 * 
 * Unauthorized copying of this file, via any medium is strictly prohibited.   
 * Proprietary and confidential
 * 
 * Last Modified: 2016-4-12 14:34:12
 ******************************************************************************/
package cn.com.xy.sms.sdk.ui.popu.util;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.util.DuoquUtils;
import cn.com.xy.sms.util.ParseManager;

public class TouchableSpan extends ClickableSpan {
    private boolean mIsPressed;
    private int mNormalTextColor = Color.BLUE;
    private int mPressedTextColor = Color.BLUE;
    
    private boolean mUnderLineMode;
    private JSONObject mParam;
    /*SDK-409 hewengao 20160426 start*/
    private Map<String, String> mExtendParam=null;
    /*SDK-409 hewengao 20160426 end*/
    
    /*SDK-605 hewengao/20161003 start*/
    private boolean mIsClickIng = false;
    /*SDK-605 hewengao/20161003 end*/
    public TouchableSpan(int normalTextColor, int pressedTextColor,
            int pressedBackgroundColor, JSONObject jobj,Map<String, String> extend) {
        mNormalTextColor = normalTextColor;
        mPressedTextColor = pressedTextColor;
        mParam = jobj;
        /*SDK-409 hewengao 20160426 start*/
        this.mExtendParam = extend;
        /*SDK-409 hewengao 20160426 end*/
    }

    public TouchableSpan(JSONObject jobj,Map<String, String> extend) {
        mParam = jobj;
        /*SDK-409 hewengao 20160426 start*/
        mExtendParam = extend;
        /*SDK-409 hewengao 20160426 end*/
    }

    public TouchableSpan(Context context, int blue, int green, int red, int i,
            Object object) {
        // TODO Auto-generated constructor stub
    }

    public void setStyle(int normalTextColor, int pressedTextColor) {
        if(normalTextColor != 0)
        mNormalTextColor = normalTextColor;
        if(pressedTextColor != 0)
        mPressedTextColor = pressedTextColor;
    }
    
    /*SDK-587 zhaojiangwei 20160812 start*/
    public void setShowUnderLine(boolean underLine){
    	mUnderLineMode = underLine;
    }
    /*SDK-587 zhaojiangwei 20160812 end*/
    
    public void setPressed(boolean isSelected) {
        mIsPressed = isSelected;
    }
    /*SDK-605 hewengao/20161003 end*/
    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setColor(mIsPressed ? mPressedTextColor : mNormalTextColor);
        /*SDK-587 zhaojiangwei 20160812 start*/
        if (mUnderLineMode) {
        	ds.setUnderlineText(true);
		}
        /*SDK-587 zhaojiangwei 20160812 end*/
        else{
        	ds.setUnderlineText(mIsPressed);
        }
    }
    /*SDK-605 hewengao/20161003 end*/
    @Override
    public void onClick(View widget) {
        /* NUBIA-109 zhaoxiachao 20161114 start */
		try {
			if (mExtendParam != null && mExtendParam.containsKey("isClickAble")) {
				String clickFlag = mExtendParam.get("isClickAble") + "";
				if ("false".equalsIgnoreCase(clickFlag)) {
					return;
				}
			}
		}
		catch (Exception e) {
			// TODO: handle exception
		}
        /* NUBIA-109 zhaoxiachao 20161114 end */
        
        try {
            /*SDK-605 hewengao/20161003 start*/
            ViewUtil.clearSpan();
            if(mIsClickIng){
                return;
            }
            mIsClickIng = true;
            /*SDK-605 hewengao/20161003 end*/
            Context context = widget.getContext();
            /*SDK-705 wangxingjian/20161222 start*/
            if (context instanceof Activity) {
               /*SDK-409 hewengao 20160426 start*/
                DuoquUtils.doAction((Activity) context, ParseManager.getRecogniseActionConfig(mParam,mExtendParam), mExtendParam);
                /*SDK-409 hewengao 20160426 end*/
            } else {
                DuoquUtils.doActionContext(context, ParseManager.getRecogniseActionConfig(mParam,mExtendParam), mExtendParam);
            }
            /*SDK-705 wangxingjian/20161222 end*/
        } catch (Throwable e) {
            LogManager.e("XIAOYUAN", "TouchableSpan on clicked error:" , e);
        }finally{
            mIsClickIng = false;
        }
    }
}
